﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class anonymous_initiator_side : BaseForm
    {
        public anonymous_initiator_side()
            : base()
        {
        }

        public anonymous_initiator_side(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("BEGIN TRY");
            sb.AppendLine("   BEGIN TRANSACTION");
            sb.AppendLine("   CREATE USER [" + tbUserName.Text + "] WITHOUT LOGIN");
            sb.AppendLine("   CREATE CERTIFICATE [" + tbCertificate.Text + "]");
            sb.AppendLine("      AUTHORIZATION [" + tbUserName.Text + "]");
            sb.AppendLine("      FROM FILE = '" + tbBackup.Text + "';");
            sb.AppendLine("   CREATE REMOTE SERVICE BINDING [" + tbRemote.Text + "]");
            sb.AppendLine("      TO SERVICE '" + tbService.Text + "' ");
            sb.AppendLine("      WITH USER = [" + tbUserName.Text + "],");
            sb.AppendLine("      ANONYMOUS = ON");
            sb.AppendLine("   COMMIT TRANSACTION");
            sb.AppendLine("END TRY");
            sb.AppendLine("BEGIN CATCH");
            sb.AppendLine("   ROLLBACK TRANSACTION");
            sb.AppendLine("   DECLARE @e nvarchar(max)");
            sb.AppendLine("   SET @e = error_message()");
            sb.AppendLine("   RAISERROR('%s', 16, 1, @e)");
            sb.AppendLine("END CATCH");

            tbCommand.Text = sb.ToString();
        }

        private void tbUserName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbService_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbCertificate_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbSubject_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbStart_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbBackup_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.AddExtension = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = ".cer";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbBackup.Text = dialog.FileName;
            }
        }

        private void tbRemote_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbService_TextChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}